package com.eqdd.yiqidian.box.ItemDecoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.utils.DensityUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DashDividerLineItemDecoration extends RecyclerView.ItemDecoration {


    //分割线画笔
    private Paint mPaint;

    //分割线距离左边距离
    private int leftPadding = 0;
    //分割线距离右边距离
    private int rightPadding = 0;

    private boolean isFirstDraw = true;
    private int dash = 30;
    private int strokeWidth = 3;


    public DashDividerLineItemDecoration setLeftPadding(int leftPadding) {
        this.leftPadding = leftPadding;
        return this;
    }

    public DashDividerLineItemDecoration setRightPadding(int rightPadding) {
        this.rightPadding = rightPadding;
        return this;
    }

    public DashDividerLineItemDecoration(Context context) {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setColor(0xff666666);
        mPaint.setPathEffect(new DashPathEffect(new float[]{dash, dash}, 0));
    }

    public DashDividerLineItemDecoration setDividerColor(int dividerColor) {
        mPaint.setColor(dividerColor);
        return this;
    }

    public DashDividerLineItemDecoration setDash(int dash) {
        mPaint.setPathEffect(new DashPathEffect(new float[]{dash, dash}, 0));
        return this;
    }

    public DashDividerLineItemDecoration setStrokeWidth(int strokeWidth) {
        mPaint.setStrokeWidth(strokeWidth);
        return this;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(view);
            if (!isFirstDraw && position == 0) {
                continue;
            }
            Path path = new Path();
            path.moveTo(left + leftPadding, view.getTop() - 3);
            path.lineTo(right - rightPadding, view.getTop());
            c.drawPath(path, mPaint);
        }
    }

    /**
     * 设置item分割线的size
     *
     * @param outRect outRect
     * @param view    view
     * @param parent  parent
     * @param state   state
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        if (isFirstDraw) {
            outRect.set(0, 3, 0, 0);
        } else {
            outRect.set(0, position == 0 ? 0 : 3, 0, 0);

        }
    }
}